package com.pachong.spider;

import com.common.core.WyyMusicConstant;
import com.common.core.utils.WyyMusicUtils;
import com.domain.bean.music.MusicUser;
import com.domain.bean.music.PlaylistPool;
import com.pachong.service.MusicUserService;
import com.pachong.service.impl.MusicServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:爬取用户信息的爬虫对象
 * Author : wangyu
 * Date: 2018-03-08
 * Time: 16:39
 */
@Component
public class WyyUserProcessor implements PageProcessor {
    private static Logger LOGGER= LoggerFactory.getLogger(WyyUserProcessor.class);
    @Autowired
    private MusicUserService musicUserService;

    @Override
    public void process(Page page) {
        //判断当前爬取的是否是人
        if(page.getUrl().regex(WyyMusicConstant.MUSIC_USER_URL).match()){
            String s = page.getUrl().toString();
            String[] split = s.split("id=");
            this.handleSong(page, split[1].trim());
        }
    }

    private void handleSong(Page page, String code) {
        MusicUser byCode = musicUserService.findByCode(code);
        if(byCode == null){
            LOGGER.error("数据有误!"+code);
            return;
        }
        Html userHtml = page.getHtml();

        //昵称
        String nickname = userHtml.xpath("//span[@class=\"tit f-ff2 s-fc0 f-thide\"]/text()").toString();
        if(StringUtils.isNotBlank(nickname)){
            byCode.setNickname(nickname.trim());
        }

        //等级
        String grade = userHtml.xpath("//span[@class=\"lev u-lev u-icn2 u-icn2-lev\"]/text()").toString();
        if(StringUtils.isNotBlank(grade) &&WyyMusicUtils.regexNumber(grade)){
            byCode.setGrade(Integer.valueOf(grade));
        }

        //动态数
        String event_count = userHtml.xpath("//strong[@id=\"event_count\"]/text()").toString();
        if(StringUtils.isNotBlank(event_count) && WyyMusicUtils.regexNumber(event_count)){
            byCode.setEventCount(Integer.valueOf(event_count));
        }

        //关注数
        String follow_count = userHtml.xpath("//strong[@id=\"follow_count\"]/text()").toString();
        if(StringUtils.isNotBlank(follow_count) && WyyMusicUtils.regexNumber(follow_count)){
            byCode.setFollowCount(Integer.valueOf(follow_count));
        }

        //粉丝数
        String fan_count = userHtml.xpath("//strong[@id=\"fan_count\"]/text()").toString();
        if(StringUtils.isNotBlank(fan_count) && WyyMusicUtils.regexNumber(fan_count)){
            byCode.setFanCount((Integer.valueOf(fan_count)));
        }

        //个人介绍
        String selfIntro = userHtml.xpath("//div[@class=\"inf s-fc3 f-brk\"]/text()").toString();
        selfIntro = selfIntro.replace("个人介绍：","").trim();
        if(StringUtils.isNotBlank(selfIntro)){
            byCode.setSelfIntro(selfIntro);
        }

        //所在地区
        String area = userHtml.xpath("//div[@class=\"inf s-fc3\"]/span/text()").toString();
        if(StringUtils.isNotBlank(area)){
            String[] split = area.replace("所在地区：", "").split("-");
            byCode.setProvince(split[0].trim());
            byCode.setCity(split[1].trim());
        }

        //年龄
        String age = userHtml.xpath("//div[@id=\"age\"]/span/text()").toString();
        if(StringUtils.isNotBlank(age)){
            byCode.setAge(age);
        }

        //社交连接
        List<String> socialUrls = userHtml.xpath("//div[@class=\"inf s-fc3 f-cb\"]").links().all();
        if(socialUrls != null && !socialUrls.isEmpty()){
            for (String url :socialUrls) {
                byCode.setSocialUrl(byCode.getSocialUrl()+","+url);
            }
        }

        //累积听歌
        String songNum = userHtml.xpath("//div[@id=\"rHeader\"]/h4/text()").toString();
        if(StringUtils.isNotBlank(songNum)){
            songNum = songNum.replace("累积听歌", "").replace("首", "").trim();
            if(WyyMusicUtils.regexNumber(songNum)){
                byCode.setListenTimes(Integer.valueOf(songNum));
            }
        }

        byCode.setTimes(byCode.getTimes()+1);//爬虫次数
        musicUserService.saveAndFlush(byCode);
    }

    @Override
    public Site getSite() {
        return WyyMusicUtils.createSite();
    }

    //爬虫用户
    public void start() {
        long start = System.currentTimeMillis();
        HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
        List<MusicUser> musicUsers = musicUserService.selectList(0);
        Spider.create(this)
//                .addUrl("http://music.163.com/user/home?id=557425391")
                .startUrls(musicUsers.stream().map(user -> {
                    String url = "http://music.163.com/user/home?id="+user.getCode();
                    return url;
                }).collect(Collectors.toList()))
                .setDownloader(WyyMusicUtils.getDownloader())
                .thread(WyyMusicConstant.THREAD_NUMS)
                .run();
        long end = System.currentTimeMillis();
        System.out.println("爬虫结束,耗时--->" + WyyMusicUtils.parseMillisecone(end - start));

    }
}
